home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Compendium Deluxe 2
/
LSD and 17bit Compendium Deluxe - Volume II.iso
/
a
/
prog
/
asmsrc
/
thesource-7.lha
/
Source
/
polyfit.lha
/
polyfit
/
mat.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-05-04
|
1KB
|
76 lines
#include "fit.h"
matrix MatInit(n)
register int n;
{
register int i;
register matrix mat;
mat = (matrix)Calloc(n, sizeof(matrow));
for (i = 0; i < n; i++)
mat[i] = (matrow)Calloc(n + 1, sizeof(matelm));
return (mat);
}
double MatInv(n, mat)
register int n;
register matrix mat;
{
int pivRow;
double piv, det, div, pivTmp;
matrow tmpRow;
register int p, r, c;
for (det = 1.0, p = 0; p < n; p++)
{
for (piv = mat[pivRow = p][p], r = p; r < n; r++)
if (((pivTmp = mat[r][p]) < 0 ? -pivTmp : pivTmp) > piv)
piv = mat[pivRow = r][p];
#ifndef NO_DETERM
if ((det *= piv) == 0.0)
break;
#endif
if (pivRow != p)
{
#ifndef NO_DETERM
det = - det;
#endif
tmpRow = mat[p];
mat[p] = mat[pivRow];
mat[pivRow] = tmpRow;
}
for (c = p; c <= n; c++)
mat[p][c] /= piv;
for (r = 0; r < n; r++)
if (r != p)
for (c = n; c >= p; c--)
mat[r][c] -= mat[r][p] * mat[p][c];
}
#ifndef NO_DETERM
return (det);
#endif
}
pointer Calloc(n, s)
unsigned n, s;
{
register pointer ret;
if ((ret = (pointer)calloc(n, s)) == NULL)
{
perror("Allocating matrix");
exit(1);
}
return (ret);
}